<template>
  <a-layout>
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible>
      <div class="left_tops">
        <p>管理后台</p>
        <a-popover placement="bottom">
          <template #content>
            <p @click="toaddtext">新建文章</p>
            <p @click="toaddpage">新建页面</p>
          </template>
          <!-- <template #title>
            <span>Title</span>
          </template> -->
          <a-button type="primary">+新建</a-button>
        </a-popover>
      </div>
      <!-- 渲染路由 -->
      <a-menu v-model:selectedKeys="selectedKeys" theme="dark" mode="inline" forceSubMenuRender
        v-for="(item, ind) in childrenes" :key="ind">
        <!-- 如果有子路由，则展示一个下拉框 -->
        <a-sub-menu v-if="item.children">
          <template #title>
            <span>{{ item.meta?.title }}</span>
          </template>
          <!-- 渲染下拉框下的子路由 -->
          <a-menu-item v-for="(val) in item.children" :key="val.path">
            {{ val.meta?.title }}
          </a-menu-item>
        </a-sub-menu>
        <!-- 没有子路由则直接渲染 -->
        <a-menu-item v-else :key="item.path">
          {{ item.meta?.title }}
        </a-menu-item>

      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0" class="tit_box">
        <div class="tit_right">
          <UserInfo />
        </div>

      </a-layout-header>
      <a-layout-content :style="{
        margin: '24px 16px',
        padding: '24px',
        background: '#eee',
      }">
        <router-view />
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>
<script lang="ts" setup>
import { childrenes } from "../router/route"
import { ref, watch } from "vue";

import { useRouter, useRoute } from "vue-router";
import { storeToRefs } from "pinia";
import { useEdits } from "../store/edits"
import { useLogin } from "../store/login"
import UserInfo from "../components/UserInfo.vue";
//引入pinia 
let edits = useEdits();
//初始化获取系统页面数据
edits.geteditList()
const route = useRoute();
const router = useRouter();
const logins = useLogin()
const { userobj } = storeToRefs(logins)
// console.log(userobj.value,"user")
const selectedKeys = ref<string[]>(JSON.parse(localStorage.getItem("path") || JSON.stringify(['/']))),
  collapsed = ref<boolean>(false);
//监听路由跳转
watch(selectedKeys, (newVal) => {
  router.push(newVal[0])
})
//路径发生改变切换路径 和 左侧导航栏高亮
watch(() => route.path, (val) => {
  localStorage.setItem("path", JSON.stringify([route.path]))
  selectedKeys.value = [val]
})
//新建文章
const toaddtext = () =>{
  router.push("/addtext")
}
//新建页面
const toaddpage = () =>{
  router.push("/addpage")
}
</script>
<style lang="scss">
.ant-layout {
  width: 100%;
  height: 100%;
}

#components-layout-demo-custom-trigger .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0;
  cursor: pointer;
  transition: color 0.3s;
}

#components-layout-demo-custom-trigger .trigger:hover {
  color: #1890ff;
}

#components-layout-demo-custom-trigger .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.3);
  margin: 16px;
}

.site-layout .site-layout-background {
  background: #fff;
}

.left_tops {
  height: 80px;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  p {
    // text-align: center;
    margin: 5px 0;
    font-size: 20px;
    font-weight: bold;
    color: white;
   
  }


}
.ant-popover-inner-content{
  cursor: pointer;
}
.tit_box {
  display: flex;
  justify-content: end;

  .tit_right {
    line-height: 20px;
  }
}

.ant-layout-content {
  overflow: auto;

  &::-webkit-scrollbar {
    width: 3px;
  }
}
</style>
  